//金额分转元(强制保留两位小数)
export const centToYuan = (amount: number) => {
    if(amount!==0&&!amount){
        return 0;
    }
	//var num = amount;
	let num=(amount*0.01).toString();
	num+='';
	var reg = num.indexOf('.') >-1 ? /(\d{1,3})(?=(?:\d{3})+\.)/g : /(\d{1,3})(?=(?:\d{3})+$)/g;
	num=num.replace(reg,'$1');
	num = toDecimal2(num)
	return num
}
export const toDecimal2 = (x: string) => {
  var f = parseFloat(x);
  if (isNaN(f)) {
    return "转换错误！";
  }
  var f = Math.round(Number(x) * 100) / 100;
  var s = f.toString();
  var rs = s.indexOf('.');
  if (rs < 0) {
    rs = s.length;
    s += '.';
  }
  while (s.length <= rs + 2) {
    s += '0';
  }
  return s;
}
//姓名脱敏，脱敏规则只显示最后一个字，其他用*代理
export const desensitizeName = (name:string) => {  
	if(!name){
		return ''
	}
   let maskedStr = '';  
   for (let i = 0; i < name.length; i++) {  
	   if (i !== name.length - 1) {  
		   maskedStr += '*';  
	   } else {  
		   maskedStr += name[i];  
	   }  
   }  
   return maskedStr;
}

//身份证脱敏
export const desensitizeID = (id:string) => {  
    return id.replace(/^(.{4})(?:\d+)(.{4})$/, "$1**********$2");
} 
//支付宝账号脱敏
export const desensitizeAccount = (account:string) => {  

    if(!account){
        return ''
    }

	// 判断是手机号还是邮箱地址  
	if (/^1[3-9]\d{9}$/.test(account)) {  
		// 手机号脱敏，中间4位显示*  
		if (!account) {  
		    return '';  
		  }  
		  let phoneNumber = String(account);  
		  if (phoneNumber.length === 11) {  
		    return phoneNumber.slice(0, 3) + '****' + phoneNumber.slice(8);  
		  }  
		  return phoneNumber; 
	} else if (/^\w+@\w+\.\w+$/.test(account)) {  
	 // 找到@的位置  
	     let atIndex = account.indexOf('@');  
	     // 获取@前的字符串长度  
	     let beforeAtLength = atIndex;  
	     // 计算需要隐藏的字符数，如果字符串长度是奇数，则隐藏的字符数为长度的一半，否则为一半减一  
	     let hideLength = Math.floor(beforeAtLength / 2);  
	     if (beforeAtLength % 2 !== 0) {  
	         hideLength = hideLength + 1;  
	     }  
	     // 使用*替换@前的字符串的后半部分  
	     let desensitizedEmail = account.replace(account.substring(beforeAtLength - hideLength, beforeAtLength), '*'.repeat(hideLength));  
	     return desensitizedEmail;   
	} else {  
	  // 不是手机号也不是邮箱地址，不进行脱敏处理  
	  return account;  
	}   
}  
//手机号脱敏
export const desensitizePhone = (phone:string) => {  
	if (/^1[3-9]\d{9}$/.test(phone)) {
		// 正则表达式，匹配手机号中间4位
		const regex = /(\d{3})\d{4}(\d{4})/;  
		// 使用 replace 方法替换中间4位为星号  
		const desensitizedPhone = phone.replace(regex, '$1****$2');  
		return desensitizedPhone;  
	}else{
		return phone;
	}
    
} 
//将后端返回的图片字符串转换为数组合适
export const imageConvertArray = (str:string) => {
	if(str==null||str=="")return [];
	let imgObj:any = [];
	let imgs = str.split(",");
	imgs.forEach((item:string) => {
		if(item!=="")imgObj.push(item);
	});
	return imgObj;
}
//根据缩略图获取高清图片地址
export const getHDImages = (urls:any) => {
	let newUrls:any = [];
	urls.forEach((item:string) => {
		const lastIndex = item.lastIndexOf('.');  
		if (lastIndex != -1) {  
		    item = item.substring(0, lastIndex) + '-big' + item.substring(lastIndex); // 在最后一个'.'前面添加'-big'  
		}
		newUrls.push(item);
	});
	return newUrls;
}
//是否是身份证
export const isID = (str:string) => {  
    return /^\d{6}((((((19|20)\d{2})(0[13-9]|1[012])(0[1-9]|[12]\d|30))|(((19|20)\d{2})(0[13578]|1[02])31)|((19|20)\d{2})02(0[1-9]|1\d|2[0-8])|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))0229))\d{3})|((((\d{2})(0[13-9]|1[012])(0[1-9]|[12]\d|30))|((\d{2})(0[13578]|1[02])31)|((\d{2})02(0[1-9]|1\d|2[0-8]))|(([13579][26]|[2468][048]|0[048])0229))\d{2}))(\d|X|x)$/.test(str)
}  
//是否是手机号
export const isPhone= (str:string) => { 
    return /^(?:(?:\+|00)86)?1[3-9]\d{9}$/.test(str)
}  
// 密码强度  大写字母，小写字母，数字，特殊符号 包含三项
export const isPWD= (str:string) => { 
    return /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_!@#$%^&*`~()-+=]+$)(?![a-z0-9]+$)(?![a-z\W_!@#$%^&*`~()-+=]+$)(?![0-9\W_!@#$%^&*`~()-+=]+$)[a-zA-Z0-9\W_!@#$%^&*`~()-+=]/.test(str)
}
//是否是汉字
export const isCH= (str:string) => { 
    return /^(?:[\u3400-\u4DB5\u4E00-\u9FEA\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29]|[\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0])+$/.test(str)
}  
//数字转汉字（支持0~99）
export const numToChinese = (num:number) => {  
    const map:any = {  
        0: '零',  
        1: '一',  
        2: '二',  
        3: '三',  
        4: '四',  
        5: '五',  
        6: '六',  
        7: '七',  
        8: '八',  
        9: '九'  
    };  
    if (num < 10) return map[num];  
    if (num < 100) return map[num % 10] + map[Math.floor(num / 10)];  
    // 对于100以上的数字，目前我们还没有对应的汉字转换规则，这个可以按照需求增加  
} 
//昵称脱敏
export const desensitizeNickname = (nickname:string) => {  
    if (nickname.length === 1) {  
        return nickname + '***';  
    } else {  
        return nickname.slice(0, 1) + '*'.repeat(nickname.length - 1);  
    }  
}
//特殊字符转译
export const escapeHtml = (unsafe:string) => {
	return unsafe  
	         .replace(/&/g, '&amp;')  
	         .replace(/</g, '&lt;')  
	         .replace(/>/g, '&gt;')  
	         .replace(/"/g, '&quot;')  
	         .replace(/'/g, '&#039;');  
}